package com.meeting.y.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: shoko (fbjia.com)
 * Date: 13-11-14
 * Time: 上午10:27
 * To change this template use File | Settings | File Templates.
 */
public class DateUtil {
    private static final String[] NUMBERS = {"〇", "一", "二", "三", "四", "五",
            "六", "七", "八", "九"};
    private static Date date = new Date();

    private DateUtil() {
    }

    public static Integer getIntDate(){
        return getIntDate(null,"yyyyMMdd");
    }

    public static Integer getIntMit(){
        return getIntDate(null,"Hm");
    }

    public static Integer getIntDate(Date d,String fmt){
        if(d ==null){
            d = new Date();
        }
        SimpleDateFormat format = new SimpleDateFormat(fmt);
        return Integer.valueOf(format.format(d));
    }


    /**
     * 获取下周周一
     *
     * @return
     */
    public static Date getNextWeekMonday() {
        java.util.GregorianCalendar currentDate = new java.util.GregorianCalendar();
        currentDate.add(java.util.GregorianCalendar.DATE, getMondayPlus() + 7);
        Date monday = currentDate.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(sdf.format(monday));
        } catch (java.text.ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取下周周日
     *
     * @return
     */
    public static Date getNextWeekSunday() {
        java.util.GregorianCalendar currentDate = new java.util.GregorianCalendar();
        currentDate.add(java.util.GregorianCalendar.DATE, getMondayPlus() + 7 + 6);
        Date sunday = currentDate.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(sdf.format(sunday));
        } catch (java.text.ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获得当前日期与本周日相差的天数
     *
     * @return
     */
    private static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1;         //因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            return 0;
        } else {
            return 1 - dayOfWeek;
        }
    }

    /**
     * 获取季度日期
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getQuarterDate(int year, int quarter) {
        Calendar cldr = Calendar.getInstance();
        cldr.set(Calendar.MONTH, quarter * 3 - 1);
        cldr.set(Calendar.YEAR, year);
        switch (quarter) {
            case 1:
                cldr.set(Calendar.DATE, 31);
                break;
            case 2:
                cldr.set(Calendar.DATE, 30);
                break;
            case 3:
                cldr.set(Calendar.DATE, 30);
                break;
            case 4:
                cldr.set(Calendar.DATE, 31);
                break;
        }
        return truncate(cldr.getTime(), Calendar.DATE);
    }

    /**
     * 变更日期精度
     *
     * @param date
     * @param type
     * @return
     */
    public static Date truncate(Date date, int type) {
        Calendar cldr = Calendar.getInstance();
        cldr.setTime(date);
        Integer[] types = new Integer[]{Calendar.YEAR, Calendar.MONTH, Calendar.DATE, Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND};
        for (Integer t : types) {
            if (t > type) {
                cldr.set(t, 0);
            }
        }
        return cldr.getTime();
    }

    /**
     * 清除时分秒
     *
     * @param date
     */
    public static Date clearTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format.parse(format.format(date));
        } catch (java.text.ParseException e) {

        }
        return date;
    }

    /**
     * 通过 yyyy-MM-dd 得到中文大写格式 yyyy年MM月dd日 日期
     *
     * @param date 目标日期
     */
    public static String toChinese(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        String str = format.format(date);
        StringBuffer sb = new StringBuffer();
        sb.append(getSplitDateStr(str, 0)).append("年").append(
                getSplitDateStr(str, 1)).append("月").append(
                getSplitDateStr(str, 2)).append("日");
        return sb.toString();
    }

    /**
     * 通过 yyyy-MM-dd 得到中文大写格式 yyyy年MM月dd日 日期
     *
     * @param str 日期 yyyy-MM-dd
     */
    public static String toChinese(String str) {
        StringBuffer sb = new StringBuffer();
        sb.append(getSplitDateStr(str, 0)).append("年").append(
                getSplitDateStr(str, 1)).append("月").append(
                getSplitDateStr(str, 2)).append("日");
        return sb.toString();
    }

    /**
     * 分别得到年月日的大写 默认分割符 "-"
     */
    public static String getSplitDateStr(String str, int unit) {
        // unit是单位 0=年 1=月 2日
        String[] DateStr = str.split("-");
        if (unit > DateStr.length)
            unit = 0;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < DateStr[unit].length(); i++) {

            if ((unit == 1 || unit == 2) && Integer.valueOf(DateStr[unit]) > 9) {
                sb.append(convertNum(DateStr[unit].substring(0, 1)))
                        .append("十").append(
                        convertNum(DateStr[unit].substring(1, 2)));
                break;
            } else {
                sb.append(convertNum(DateStr[unit].substring(i, i + 1)));
            }
        }
        if (unit == 1 || unit == 2) {
            return sb.toString().replaceAll("^一", "").replace("〇", "");
        }
        return sb.toString();

    }

    /**
     * 转换数字为大写
     */
    private static String convertNum(String str) {
        return NUMBERS[Integer.valueOf(str)];
    }
}
